#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: contact
    Description:
        Author: lhw
        Date: 2020/9/11
-----------------------------------
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Baby import models
from Baby.serializers import contact
from Common.logger import app_log
from Common.response import resp


class ContactView(APIView):
    # authentication_classes = [JwtAuthorizationAuthentication,]

    def get(self, request, *args, **kwargs):
        try:
            baby_id = request.query_params.get("id", "")
            hospital = request.query_params.get("hospital", "")
            contact_query = models.Contact.objects.filter(babies=baby_id, hospital=hospital).all()
            contact_data = contact.ContactModelSerializer(contact_query, many=True).data
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": contact_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            contact_ser = contact.ContactModelSerializer(data=request_data)
            contact_ser.is_valid(raise_exception=True)
            contact_obj = contact_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": contact.ContactModelSerializer(contact_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            contact_id = request_data["id"]
            old_contact_obj = models.Contact.objects.filter(id=contact_id).first()
            contact_ser = contact.ContactModelSerializer(instance=old_contact_obj,
                                                         data=request_data, partial=True)
            contact_ser.is_valid(raise_exception=True)
            contact_obj = contact_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": contact.ContactModelSerializer(contact_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            contact_id = request.GET.get("id")
            contact_data = models.Contact.objects.filter(id=contact_id).first()
            if contact_data:
                models.Contact.objects.filter(id=contact_id).delete()
                return Response(
                    {
                        "error_code": 0,
                        "message": "Success"
                    }
                )
            else:
                return resp(2005, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e, args)
